package com.peas.platform.module.icims.expenditure.expenses.domain.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialCashDto;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialCashService;
import com.peas.platform.module.icims.expenditure.expenses.adapter.repository.ExpenditureReimbursementContractRepository;
import com.peas.platform.module.icims.expenditure.expenses.adapter.repository.po.ExpenditureReimbursementContractPo;
import com.peas.platform.module.icims.expenditure.expenses.domain.ExpenditureReimbursementContractDomain;
import com.peas.platform.module.icims.expenditure.expenses.dto.ExpenditureReimbursementContractDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class ExpenditureReimbursementContractDomainImpl extends BaseDomainImpl<ExpenditureReimbursementContractDto, ExpenditureReimbursementContractPo> implements ExpenditureReimbursementContractDomain {

    @Resource
    private ExpenditureReimbursementContractRepository expenditureReimbursementContractRepository;
    @Resource
    private ContractInitialCashService contractInitialCashService;

    @Override
    public BaseRepository<ExpenditureReimbursementContractPo> getMapper() {
        return expenditureReimbursementContractRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureReimbursementContractPo>> getRepositoryClass() {
        return ExpenditureReimbursementContractRepository.class;
    }

    @Transactional
    @Override
    public int save(ExpenditureReimbursementContractDto dto) {
        int result = super.save(dto);
        if (dto.getContractInitialCashId() != null) {
            ContractInitialCashDto cash = contractInitialCashService.getById(dto.getContractInitialCashId());
            Assert.notNull(cash, "找不到合同请款记录");
            Assert.equals("2", cash.getState(), "合同[{}]的请款状态不是请款中", dto.getContractNo());
            cash.setState("3");
            contractInitialCashService.updateById(cash);
        }
        return result;
    }

    @Transactional
    @Override
    public boolean saveBatch(List<ExpenditureReimbursementContractDto> list) {
        if (CollUtil.isEmpty(list)) {
            return false;
        }
        list.forEach(this::save);
        return true;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        ExpenditureReimbursementContractDto dto = this.getById(id);
        if (dto.getContractInitialCashId() != null) {
            ContractInitialCashDto cash = contractInitialCashService.getById(dto.getContractInitialCashId());
            Assert.notNull(cash, "找不到合同请款记录");
            cash.setState("2");
            contractInitialCashService.updateById(cash);
        }
        return super.removeById(id);
    }

    @Transactional
    @Override
    public boolean removeBatchByList(List<ExpenditureReimbursementContractDto> list) {
        if (CollUtil.isEmpty(list)) {
            return false;
        }
        list.forEach(dto -> this.removeById(dto.getId()));
        return true;
    }

}
